﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class NhomNguoiDungProvider
    {
        public static List<NhomNguoiDung> LayDanhSachNhomNguoiDung()
        {
            var lst = new List<NhomNguoiDung>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.NhomNguoiDungs select u);

                foreach (var row in query)
                {
                    var nhomND = new NhomNguoiDung();
                    nhomND.MaNhomND = row.MaNhomND;
                    nhomND.TenNhom = row.TenNhom;
                    nhomND.MoTa = row.MoTa;

                    lst.Add(nhomND);
                }
            }

            return lst;
        }

        public static bool XoaNhomNguoiDung(int maNhomND)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {

                    var query = (from u in context.NhomNguoiDungs
                                 where u.MaNhomND.Equals(maNhomND)
                                 select u).First();

                    context.NhomNguoiDungs.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatNhomNguoiDung(NhomNguoiDung nhomND)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.NhomNguoiDungs
                                 where u.MaNhomND.Equals(nhomND.MaNhomND)
                                 select u).First();
                    query.TenNhom = nhomND.TenNhom;
                    query.MoTa = nhomND.MoTa;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static int ThemNhomNguoiDung(NhomNguoiDung nhomND)
        {
            int maNhomND = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.NhomNguoiDungs.Add(nhomND);
                    context.SaveChanges();
                    maNhomND = nhomND.MaNhomND;
                }
            }
            catch
            {

            }
            return maNhomND;
        }
    }
}
